package elephant.service.db;

import java.util.ArrayList;
import java.util.List;

import elephant.jdbc.DataSourceManager;
import elephant.jdbc.datasource.DBDataSource;
import elephant.service.Service;
import elephant.util.ObjectPrinter;

/**
 * 
 * @author skydu
 *
 */
public class JdbcService extends Service{

	private List<DBDataSource> dataSources;
	//
	public JdbcService(){
		dataSources=new ArrayList<>();
	}
	//
	@Override
	public void init() throws Exception {
		for (DBDataSource dataSource : dataSources) {
			dataSource.init();
			DataSourceManager.addDataSource(dataSource);
		}
	}

	@Override
	public void start() throws Exception {
		for (DBDataSource dataSource : dataSources) {
			dataSource.start();
		}
	}

	@Override
	public void stop() throws Exception {
		for (DBDataSource dataSource : dataSources) {
				dataSource.stop();
		}
	}

	@Override
	public String dump() {
		ObjectPrinter op=new ObjectPrinter();
		op.section(getClass().getSimpleName());
		int index=0;
		for (DBDataSource dataSource : dataSources) {
			op.secondSection(index+++"");
			op.println(dataSource.toString());
		}
		return op.toString();
	}

	/**
	 * 
	 * @param dataSource
	 */
	public void addDataSource(DBDataSource dataSource) {
		dataSources.add(dataSource);
	}
}
